package cc.linker.steplink.crm.service.impl;

import cc.linker.steplink.crm.dao.AwakenDao;
import cc.linker.steplink.crm.domain.Awaken;
import cc.linker.steplink.crm.service.IAwakenService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class AwakenServiceImpl implements IAwakenService {

    @Autowired
    private AwakenDao awakenDao;


    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public List<Map<String, Object>> listAwaken(Map map) {
        return awakenDao.listAwaken(map);
    }

    @Override
    public Boolean addAwaken(Awaken wake,String labels) throws Exception {
        String orgId = wake.getOrgId();
        String taskId = wake.getTaskId();
        String classId = wake.getArticleId();
        String key = "sync_awakenarticle_list";
        if(awakenDao.insertSelective(wake) == 1){
            Map <String,String>mapda = new HashMap<String,String>();
            mapda.put("taskId",taskId);
            mapda.put("orgId",orgId);
            mapda.put("classId",classId);
            mapda.put("labels",labels);
            JSONObject jsonObject = (JSONObject)JSONArray.toJSON(mapda);
            stringRedisTemplate.opsForList().rightPush(key, jsonObject.toJSONString());
            return true;
        }
        return false;
    }

    @Override
    public Boolean delAwaken(Awaken wake) throws Exception {
        if(awakenDao.delAwaken(wake) == 1){
            awakenDao.delAwakenArticle(wake.getTaskId());
            stringRedisTemplate.delete("task_send_time_"+wake.getTaskId());
            return true;
        }
        return false;
    }

    @Override
    public Boolean editAwaken(Awaken wake,String labels) throws Exception {
        String orgId = wake.getOrgId();
        String taskId = wake.getTaskId();
        String classId = wake.getArticleId();
        String key = "sync_awakenarticle_list";
        if(awakenDao.editAwaken(wake) == 1){
            Map <String,String>mapda = new HashMap<String,String>();
            mapda.put("taskId",taskId);
            mapda.put("orgId",orgId);
            mapda.put("classId",classId);
            mapda.put("labels",labels);
            JSONObject jsonObject = (JSONObject)JSONArray.toJSON(mapda);
            stringRedisTemplate.opsForList().rightPush(key, jsonObject.toJSONString());
            stringRedisTemplate.delete("task_send_time_"+taskId);
            return true;
        }
        return false;
    }

    @Override
    public Boolean addAwakenArticle(Map map) throws Exception {
        Integer articleCount = awakenDao.getArticleCount(map);
        if(articleCount>0){
            return true;
        }
        return awakenDao.addAwakenArticle(map) == 1;
    }

    @Override
    public Boolean delAwakenArticle(String taskId) throws Exception {
        return awakenDao.delAwakenArticle(taskId) == 1;
    }

    @Override
    public List<Map<String, Object>> getAwaken(Map map) {
        return awakenDao.getAwaken(map);
    }

}
